package com.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.domain.Data;
import com.cms.domain.Item;
import com.cms.service.DataRowService;
import com.cms.service.DataService;
import com.cms.service.ItemService;

@Service
public class DataRowServiceImpl implements DataRowService{
	
	@Autowired
	private ItemService itemService;
	
	@Autowired
	private DataService dataService;
	
	public LinkedHashMap<String, Data> getByRowNum(Long id_sbj, Long rowNum){
		LinkedHashMap<String, Data> mapRow = new LinkedHashMap();
		
		//查询列头
		List<Item> itemList = itemService.getList(id_sbj);

		//查询数据
		List<Data> datas = dataService.getRowList(id_sbj, rowNum);

		//组装数据
		List<LinkedHashMap<String, Data>> list = this.packageData(itemList, datas);
		if(list!=null && list.size()>0){
			mapRow = list.get(0);
		}
		
		return mapRow;
	}
	
	public LinkedHashMap<String, String> getValuesByRowNum(Long id_sbj, Long rowNum){
		LinkedHashMap<String, String> mapRow = new LinkedHashMap();
		
		//查询列头
		List<Item> itemList = itemService.getList(id_sbj);

		//查询数据
		List<Data> datas = dataService.getRowList(id_sbj, rowNum);
		
		//组装数据
		List<LinkedHashMap<String, String>> list = this.packageDataValue(itemList, datas);
		if(list!=null && list.size()>0){
			mapRow = list.get(0);
		}
		
		return mapRow;
	}
	
	public String getValueByRowNum(Long id_sbj, Long rowNum, String item_name){
		
		List<Item> itemList = itemService.getByName(id_sbj, item_name);
		if(itemList==null || itemList.size()<1){
			return null;
		}
		Item item = itemList.get(0);
		
		Data data = dataService.getByIds(id_sbj, rowNum, item.getId());
		if(data==null){
			return null;
		}
		
		if("blob".equals(item.getType())){
			return data.getValueBlob();
		}else{
			return data.getValue();
		}
	}
	
	public List<LinkedHashMap<String, Data>> getList(Long id_sbj){

		List<LinkedHashMap<String, Data>> dataRowList = new ArrayList();
		
		//查询列头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		
		//查询数据
		List<Data> datas = dataService.getList(Long.valueOf(id_sbj));

		//组装数据
		dataRowList = this.packageData(itemList, datas);

		return dataRowList;
	}
	

	public List<LinkedHashMap<String, Data>> getList(Long id_sbj, Map params){

		List<LinkedHashMap<String, Data>> dataRowList = new ArrayList();
		
		//查询列头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		HashMap<String, Item> itemMap = new HashMap();
		for(Item item:itemList){
			itemMap.put(item.getName(), item);
		}
		
		//获取对应的rowNum
		List<Map> rowNum_list = dataService.getRowCountMap(id_sbj, itemMap, params);
		
		//查询数据
		List<Data> datas = new ArrayList();
		for(Map rowNum_map:rowNum_list){
			Integer count = Integer.parseInt(rowNum_map.get("DATA_COUNT").toString());
			Long rowNum = Long.parseLong(rowNum_map.get("ROWNUM").toString());
			if(count>=params.size()){
				List<Data> data_list = dataService.getRowList(id_sbj, rowNum);
				datas.addAll(data_list);
			}
		}

		//组装数据
		dataRowList = this.packageData(itemList, datas);

		return dataRowList;
	}
	
	
	public List<LinkedHashMap<String, String>> getValuesList(Long id_sbj){

		List<LinkedHashMap<String, String>> dataRowList = new ArrayList();
		
		//查询列头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		
		//查询数据
		List<Data> datas = dataService.getList(Long.valueOf(id_sbj));

		//组装数据
		dataRowList = this.packageDataValue(itemList, datas);
		
		return dataRowList;
	}
	
	public List<LinkedHashMap<String, String>> getValuesList(Long id_sbj, Map params){

		List<LinkedHashMap<String, String>> dataRowList = new ArrayList();
		
		//查询列头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		HashMap<String, Item> itemMap = new HashMap();
		for(Item item:itemList){
			itemMap.put(item.getName(), item);
		}
		
		//获取对应的rowNum
		List<Map> rowNum_list = dataService.getRowCountMap(id_sbj, itemMap, params);
		
		//查询数据
		List<Data> datas = new ArrayList();
		for(Map rowNum_map:rowNum_list){
			Integer count = Integer.parseInt(rowNum_map.get("DATA_COUNT").toString());
			Long rowNum = Long.parseLong(rowNum_map.get("ROWNUM").toString());
			if(count>=params.size()){
				List<Data> data_list = dataService.getRowList(id_sbj, rowNum);
				datas.addAll(data_list);
			}
		}

		//组装数据
		dataRowList = this.packageDataValue(itemList, datas);
		
		return dataRowList;
	}
	
	public List<LinkedHashMap<String, Data>> getList(Long id_sbj, int page_num, int page_size){
		List<LinkedHashMap<String, Data>> dataRowList = new ArrayList();
		
		//计算起止位置
		HashMap<String, Integer> map_offset = this.calculateOffset(id_sbj, page_num, page_size);
		int startIndex = map_offset.get("startIndex");
		int size = map_offset.get("size");
		
		//获取题头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		
		//组装数据
		List<Data> datas = dataService.getList(id_sbj, startIndex, size);

		//组装数据
		dataRowList = this.packageData(itemList, datas);
		
		return dataRowList;
	}
	

	public List<LinkedHashMap<String, Data>> getList(Long id_sbj, int page_num, int page_size, Map params){
		List<LinkedHashMap<String, Data>> dataRowList = new ArrayList();
		
		//计算起止位置
		HashMap<String, Integer> map_offset = this.calculateOffset(id_sbj, page_num, page_size);
		int startIndex = 0;
		if(page_num>1){
			startIndex = (page_num-1)*page_size;
		}
		
		//查询列头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		HashMap<String, Item> itemMap = new HashMap();
		for(Item item:itemList){
			itemMap.put(item.getName(), item);
		}
		
		//获取对应的rowNum
		List<Map> rowNum_list = dataService.getRowCountMap(id_sbj, itemMap, params);

		int size = 0;
		int startCount=0;
		//查询数据
		List<Data> datas = new ArrayList();
		for(Map rowNum_map:rowNum_list){
			Integer count = Integer.parseInt(rowNum_map.get("DATA_COUNT").toString());
			Long rowNum = Long.parseLong(rowNum_map.get("ROWNUM").toString());
			if(count>=params.size()){
				if(startCount>=startIndex){
					List<Data> data_list = dataService.getRowList(id_sbj, rowNum);
					datas.addAll(data_list);
					size ++;
					if(size==page_size){
						break;
					}
				}
				startCount++;
			}
		}

		//组装数据
		dataRowList = this.packageData(itemList, datas);
		
		return dataRowList;
	}
	
	public List<LinkedHashMap<String, String>> getValuesList(Long id_sbj, int page_num, int page_size){
		List<LinkedHashMap<String, String>> dataRowList = new ArrayList();

		//计算起止位置
		HashMap<String, Integer> map_offset = this.calculateOffset(id_sbj, page_num, page_size);
		int startIndex = map_offset.get("startIndex");
		int size = map_offset.get("size");
		
		//获取题头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		
		//组装数据
		List<Data> datas = dataService.getList(id_sbj, startIndex, size);

		//组装数据
		dataRowList = this.packageDataValue(itemList, datas);
		
		return dataRowList;
	}
	
	public List<LinkedHashMap<String, String>> getValuesList(Long id_sbj, int page_num, int page_size, Map params){
		List<LinkedHashMap<String, String>> dataRowList = new ArrayList();

		//计算起止位置
		HashMap<String, Integer> map_offset = this.calculateOffset(id_sbj, page_num, page_size);
		int startIndex = 0;
		if(page_num>1){
			startIndex = (page_num-1)*page_size;
		}
		
		//查询列头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		HashMap<String, Item> itemMap = new HashMap();
		for(Item item:itemList){
			itemMap.put(item.getName(), item);
		}
		
		//获取对应的rowNum
		List<Map> rowNum_list = dataService.getRowCountMap(id_sbj, itemMap, params);

		int size = 0;
		int startCount=0;
		//查询数据
		List<Data> datas = new ArrayList();
		for(Map rowNum_map:rowNum_list){
			Integer count = Integer.parseInt(rowNum_map.get("DATA_COUNT").toString());
			Long rowNum = Long.parseLong(rowNum_map.get("ROWNUM").toString());
			if(count>=params.size()){
				if(startCount>=startIndex){
					List<Data> data_list = dataService.getRowList(id_sbj, rowNum);
					datas.addAll(data_list);
					size ++;
					if(size==page_size){
						break;
					}
				}
				startCount++;
			}
		}
		
		//组装数据
		dataRowList = this.packageDataValue(itemList, datas);
		
		return dataRowList;
	}
	
	
	public int getRowCount(Long id_sbj){
		return dataService.getRowCount(id_sbj);
	}
	
	public int getRowCount(Long id_sbj, Map params){
		int rowCount = 0;
		
		//查询列头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		HashMap<String, Item> itemMap = new HashMap();
		for(Item item:itemList){
			itemMap.put(item.getName(), item);
		}
		
		//获取对应的rowNum
		List<Map> rowNum_list = dataService.getRowCountMap(id_sbj, itemMap, params);
		
		//查询数据
		for(Map rowNum_map:rowNum_list){
			Integer count = Integer.parseInt(rowNum_map.get("DATA_COUNT").toString());
			if(count>=params.size()){
				rowCount++;
			}
		}

		return rowCount;
	}
	
	
	private HashMap<String, Integer> calculateOffset(Long id_sbj, int page_num, int page_size){
		int startRow = 0;
		if(page_num>=1){
			startRow = (page_num-1)*page_size;
		}
		
		int startIndex = 0;
		int size = 0;
		
		List<Map> count_list = dataService.getRowCountMap(id_sbj);
		for(int i = 0;i<startRow; i++){
			if(count_list.size()<=i){
				break;
			}
			Map count_map = count_list.get(i);
			if(count_map.get("DATA_COUNT")!=null){
				startIndex =  startIndex + Integer.parseInt(count_map.get("DATA_COUNT").toString());
			}
		}
		for(int j = startRow; j<(startRow+page_size);j++){
			if(count_list.size()<=j){
				break;
			}
			Map count_map = count_list.get(j);
			if(count_map.get("DATA_COUNT")!=null){
				size =  size + Integer.parseInt(count_map.get("DATA_COUNT").toString());
			}
		}
		
		HashMap<String, Integer> map_offset = new HashMap();
		map_offset.put("startIndex", startIndex);
		map_offset.put("size", size);
		
		return map_offset;
	}
	
	private List<LinkedHashMap<String, Data>> packageData(List<Item> itemList, List<Data> datas){
		List<LinkedHashMap<String, Data>> dataRowList = new ArrayList();
		
		//查询列头
		HashMap<Long , String> item_name = new HashMap();
		for(Item item:itemList){
			item_name.put(item.getId(), item.getName());
		}
		
		//查询数据
		HashMap<Integer , Integer> num_row_hm = new HashMap();
		for(Data data:datas){
			int data_num = data.getRowNum().intValue();
			if(item_name.get(data.getIdItem())!=null){
				LinkedHashMap<String, Data> mapRow = new LinkedHashMap();
				if(num_row_hm.get(data_num)!=null){
					mapRow = dataRowList.get(num_row_hm.get(data_num));
				}else{
					dataRowList.add(mapRow);
					num_row_hm.put(data_num, dataRowList.size()-1);
				}
				mapRow.put(item_name.get(data.getIdItem()), data);
			}
		}
		
		return dataRowList;
	}
	
	private List<LinkedHashMap<String, String>> packageDataValue(List<Item> itemList, List<Data> datas){
		List<LinkedHashMap<String, String>> dataRowList = new ArrayList();
		
		//查询列头
		HashMap<Long , String> item_name = new HashMap();
		HashMap<Long , String> item_type = new HashMap();
		for(Item item:itemList){
			item_name.put(item.getId(), item.getName());
			item_type.put(item.getId(), item.getType());
		}
		
		//查询数据
		HashMap<Integer , Integer> num_row_hm = new HashMap();
		
		for(Data data:datas){
			int data_num = data.getRowNum().intValue();
			if(item_name.get(data.getIdItem())!=null){
				LinkedHashMap<String, String> mapRow = new LinkedHashMap();
				if(num_row_hm.get(data_num)!=null){
					mapRow = dataRowList.get(num_row_hm.get(data_num));
				}else{
					mapRow.put("rowNum", data.getRowNum().toString());
					dataRowList.add(mapRow);
					num_row_hm.put(data_num, dataRowList.size()-1);
				}
				if("blob".equals(item_type.get(data.getIdItem()))){
					mapRow.put(item_name.get(data.getIdItem()), data.getValueBlob());
				}else{
					mapRow.put(item_name.get(data.getIdItem()), data.getValue());
				}
			}
		}
		
		return dataRowList;
	}
	

}
